<script setup>
import { inject, computed, ref } from 'vue'


// 注入数据
const memberData = inject('memberData', [])
const loading = inject('loading', true)

// 折叠面板的活跃项
const activeNames = ref([])

// 按title分类分组的成员数据
const membersByTitle = computed(() => {
  const grouped = {}
  memberData.value.forEach(member => {
    // 使用 title 字段进行分组，如果没有 title 字段，则使用 classify 字段
    const groupKey = member.title || member.classify || '未分类'
    if (!grouped[groupKey]) {
      grouped[groupKey] = []
    }
    grouped[groupKey].push(member)
  })
  return grouped
})

// 格式化学术成果文本，添加序号并分行显示
const formatAchievementText = (text) => {
  if (!text) return ''
  
  // 按换行符或句号分割文本
  const items = text.split(/[\n。；;]/).filter(item => item.trim())
  
  if (items.length <= 1) {
    return text
  }
  
  // 为每个条目添加序号
  return items.map((item, index) => {
    const trimmedItem = item.trim()
    if (trimmedItem) {
      return `[${index + 1}] ${trimmedItem}`
    }
    return ''
  }).filter(item => item).join('<br/>')
}

</script>

<template>
  <div class="team-container">
    <h1 class="team-title">团队成员</h1>
    
    <div v-if="loading" class="loading-container">
      <el-skeleton :rows="5" animated />
    </div>
    
    <div v-else-if="Object.keys(membersByTitle).length === 0" class="empty-state">
      <p>暂无团队成员数据</p>
    </div>
    
    <div v-else>
      <div v-for="(members, category) in membersByTitle" :key="category" class="team-section">
        <h2 class="category-title">{{ category }}</h2>
        <div class="members-grid">
          <div v-for="member in members" :key="member.id" class="member-card">
            <div class="member-basic-info">
              <div class="member-avatar">
                <img :src="member.avatar || '/default-avatar.png'" :alt="member.name" />
              </div>
              <div class="member-basic-details">
                <h3 class="member-name">{{ member.name }}</h3>
                <p v-if="member.classify" class="member-classify"><span class="label">分类：</span>{{ member.classify }}</p>
                <p v-if="member.grade" class="member-grade"><span class="label">年级：</span>{{ member.grade }}</p>
                <p v-if="member.researchInterest" class="member-research"><span class="label">研究方向：</span>{{ member.researchInterest }}</p>
              </div>
            </div>
            
            <el-collapse v-model="activeNames" class="member-collapse">
              <el-collapse-item :title="'查看详细信息'" :name="`member-${member.id}`">
                <div class="member-detailed-info">
                  <div class="member-status-section" v-if="member.currentStatus">
                    <p class="member-status"><span class="label">当前状态：</span>{{ member.currentStatus }}</p>
                  </div>
                  
                  <div class="member-contact" v-if="member.phone || member.email">
                    <h4 class="section-title">联系方式</h4>
                    <p v-if="member.phone" class="contact-item"><span class="label">电话：</span>{{ member.phone }}</p>
                    <p v-if="member.email" class="contact-item"><span class="label">邮箱：</span>{{ member.email }}</p>
                  </div>
                  
                  <div class="member-achievements" v-if="member.thesis || member.project || member.contest || member.reward">
                    <h4 class="section-title">学术成果</h4>
                    <div v-if="member.thesis" class="achievement-section">
                      <h5 class="achievement-title">论文成果</h5>
                      <div class="achievement-content" v-html="formatAchievementText(member.thesis)"></div>
                    </div>
                    <div v-if="member.project" class="achievement-section">
                      <h5 class="achievement-title">项目经历</h5>
                      <div class="achievement-content" v-html="formatAchievementText(member.project)"></div>
                    </div>
                    <div v-if="member.contest" class="achievement-section">
                      <h5 class="achievement-title">竞赛经历</h5>
                      <div class="achievement-content" v-html="formatAchievementText(member.contest)"></div>
                    </div>
                    <div v-if="member.reward" class="achievement-section">
                      <h5 class="achievement-title">获奖情况</h5>
                      <div class="achievement-content" v-html="formatAchievementText(member.reward)"></div>
                    </div>
                  </div>
                </div>
              </el-collapse-item>
            </el-collapse>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.team-container {
  padding: 2rem;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.team-title {
  font-size: 1.8rem;
  color: #333;
  margin-bottom: 1.5rem;
  padding-bottom: 0.8rem;
  border-bottom: 2px solid #eaeaea;
  position: relative;
}

.team-title::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 80px;
  height: 2px;
  background-color: #409EFF;
}

.loading-container {
  padding: 1rem 0;
}

.empty-state {
  text-align: center;
  padding: 3rem;
  color: #909399;
  font-size: 1.2rem;
}

.team-section {
  margin-bottom: 2.5rem;
}

.category-title {
  font-size: 1.4rem;
  color: #409EFF;
  margin-bottom: 1rem;
  padding-left: 0.5rem;
  border-left: 4px solid #409EFF;
}

.members-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 1.5rem;
}

.member-card {
  background: #fff;
  border: 1px solid #ebeef5;
  border-radius: 8px;
  padding: 1.5rem;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.member-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  border-color: #409EFF;
}

.member-basic-info {
  display: flex;
  align-items: center;
  margin-bottom: 1rem;
  gap: 1rem;
}

.member-avatar {
  flex-shrink: 0;
}

.member-avatar img {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid #f0f0f0;
  transition: border-color 0.3s ease;
}

.member-card:hover .member-avatar img {
  border-color: #409EFF;
}

.member-basic-details {
  flex: 1;
}

.member-name {
  font-size: 1.4rem;
  color: #333;
  margin-bottom: 0.8rem;
  font-weight: 600;
}

.member-basic-details p {
  margin: 0.3rem 0;
  font-size: 1rem;
  line-height: 1.5;
}

.label {
  font-weight: 600;
  color: #409EFF;
  margin-right: 0.5rem;
}

.member-collapse {
  margin-top: 1rem;
}

.member-detailed-info {
  padding: 1rem 0;
}

.member-status-section {
  margin-bottom: 1rem;
}

.member-status {
  font-size: 1rem;
  color: #67C23A;
  margin: 0;
}

.section-title {
  font-size: 1.1rem;
  color: #333;
  margin-bottom: 0.8rem;
  font-weight: 600;
  border-bottom: 1px solid #eaeaea;
  padding-bottom: 0.5rem;
}

.member-contact {
  margin-bottom: 1.5rem;
}

.contact-item {
  margin: 0.5rem 0;
  font-size: 1rem;
  color: #333;
}

.member-achievements {
  margin-bottom: 0;
}

.achievement-section {
  margin-bottom: 1.2rem;
}

.achievement-section:last-child {
  margin-bottom: 0;
}

.achievement-title {
  font-size: 1rem;
  color: #409EFF;
  margin-bottom: 0.6rem;
  font-weight: 600;
}

.achievement-content {
  font-size: 0.9rem;
  color: #333;
  line-height: 1.8;
  margin: 0;
  text-align: justify;
  word-break: break-word;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .team-container {
    padding: 1.5rem;
  }
  
  .members-grid {
    grid-template-columns: 1fr;
    gap: 1rem;
  }
  
  .team-title {
    font-size: 1.5rem;
  }
  
  .category-title {
    font-size: 1.2rem;
  }
}

@media (max-width: 480px) {
  .team-container {
    padding: 1rem;
  }
  
  .member-card {
    padding: 1rem;
  }
  
  .team-title {
    font-size: 1.3rem;
  }
  
  .members-grid {
    grid-template-columns: 1fr;
  }
}
</style>